package br.ucb.cm.board.ui;

import java.awt.Color;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.border.LineBorder;
import br.ucb.cm.board.bean.Location;
import br.ucb.cm.board.util.ImageEnum;
import br.ucb.cm.config.bean.PieceConfig;
import br.ucb.cm.main.common.GeneralConstants;
import br.ucb.cm.main.common.JImagePanel;

import java.awt.Dimension;

public class SquarePane extends JImagePanel {
	private static final long serialVersionUID = 1L;
	private PieceConfig currentPiece;
	private BoardPane boardPane;
	private Location local;
	private int dimensionType;
	private int type;
	
	public static final List<ImageEnum> BKG = ImageEnum.getBkgImages();
	private PiecePane piecePane;

	public SquarePane(int dimensionType, int bkg, BoardPane boardPane2, Location location) {
		this.boardPane = boardPane2;
		this.local = location;
		this.type = bkg;
		this.dimensionType = dimensionType;
		
		Dimension dimension = new Dimension(dimensionType, dimensionType);

		fixDimension(dimension);
		setImage(BKG.get(type).getResourcePath(), dimensionType, dimensionType);
		initComponents();
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public Location getLocal() {
		return local;
	}

	public void setLocal(Location local) {
		this.local = local;
	}

	public BoardPane getController() {
		return boardPane;
	}

	public void setController(BoardPane controller) {
		this.boardPane = controller;
	}

	public PieceConfig getCurrentPiece() {
		return currentPiece;
	}

	public void setCurrentPiece(PieceConfig currentPiece) {
		this.currentPiece = currentPiece;
	}

	private void fixDimension(Dimension dimension) {
		setPreferredSize(dimension);
		setMinimumSize(dimension);
		setMaximumSize(dimension);
	}

	private void initComponents() {

		piecePane = new PiecePane();
		addMouseListener(getController());
		setBorder(new LineBorder(new Color(0, 0, 0)));
		piecePane.setOpaque(false);

		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(piecePane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(piecePane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				);


	}

	public void reset(){
		piecePane.removeImage();
		setCurrentPiece(null);
		repaint();
	}
	
	public void setPiece(PieceConfig piece) {
		setCurrentPiece(piece);
		piecePane.setImage(piece.getIcon().getResourcePath(), GeneralConstants.DEFAULT_PIECE_DIMENSION, GeneralConstants.DEFAULT_PIECE_DIMENSION);
		repaint();
	}
	
	public void select(boolean select, boolean isTarget){
		if (select)
			setImage(BKG.get(type + ((isTarget) ? 4 : 2)).getResourcePath(), dimensionType, dimensionType);
		else
			setImage(BKG.get(type).getResourcePath(), dimensionType, dimensionType);
	}
	
	public void setDimensionType(int dimensionType) {
		Dimension dimension = new Dimension(dimensionType, dimensionType);

		fixDimension(dimension);
		setImage(BKG.get(type).getResourcePath(), dimensionType, dimensionType);
		repaint();
	}
}
